<template>
  <div>
    <n-modal
      v-model:show="showModal"
      :mask-closable="false"
      :show-icon="false"
      preset="dialog"
      transform-origin="center"
      :title="formValue.id > 0 ? '编辑广告运营 #' + formValue.id : '添加广告运营'"
      :style="{
        width: dialogWidth,
      }"
    >
      <n-scrollbar style="max-height: 87vh" class="pr-5">
        <n-spin :show="loading" description="请稍候...">
          <n-form
            ref="formRef"
            :model="formValue"
            :label-placement="settingStore.isMobile ? 'top' : 'left'"
            :label-width="100"
            class="py-4"
          >
            <n-grid cols="1 s:1 m:1 l:1 xl:1 2xl:1" responsive="screen">
              <n-gi span="1">
                <n-form-item label="广告名称" path="name">
                  <n-input placeholder="请输入广告名称" v-model:value="formValue.name" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="广告图片" path="imgUrl">
                  <UploadImage :maxNumber="1" v-model:value="formValue.imgUrl" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="广告跳转" path="sourceType">
                  <n-radio-group v-model:value="formValue.sourceType" name="sourceType">
                    <n-radio-button
              v-for="sourceType in dict.getOptionUnRef('xxb_yy_ggyy_tzlx')"
              :key="sourceType.value"
              :value="sourceType.value"
              :label="sourceType.label"
            />
                  </n-radio-group>
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="商品" path="productId"  v-if="formValue.sourceType === 1">
                  <ProductSelect v-model="formValue.sourceId" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="资源地址" path="sourceUrl" v-if="formValue.sourceType === 1 || formValue.sourceType === 2">
                  <n-input placeholder="请输入资源地址" v-model:value="formValue.sourceUrl" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="广告位置" path="location">
                  <n-radio-group v-model:value="formValue.location" name="location">
                    <n-radio-button
              v-for="location in dict.getOptionUnRef('xxb_yy_ggyy_ggwz')"
              :key="location.value"
              :value="location.value"
              :label="location.label"
            />
                  </n-radio-group>
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="上线时间" path="startTime">
                  <DatePicker v-model:formValue="formValue.startTime" type="datetime" />
                </n-form-item>
              </n-gi>
            </n-grid>
          </n-form>
        </n-spin>
      </n-scrollbar>
      <template #action>
        <n-space>
          <n-button @click="closeForm">
            取消
          </n-button>
          <n-button type="info" :loading="formBtnLoading" @click="confirmForm">
            确定
          </n-button>
        </n-space>
      </template>
    </n-modal>
  </div>
</template>

<script lang="ts" setup>
  import { Edit, View } from '@/api/opsBanners';
import DatePicker from '@/components/DatePicker/datePicker.vue';
import UploadImage from '@/components/Upload/uploadImage.vue';
import { useDictStore } from '@/store/modules/dict';
import { useProjectSettingStore } from '@/store/modules/projectSetting';
import { adaModalWidth } from '@/utils/hotgo';
import { useMessage } from 'naive-ui';
import { computed, ref } from 'vue';
import { State, newState } from './model';

  const emit = defineEmits(['reloadTable']);
  const message = useMessage();
  const settingStore = useProjectSettingStore();
  const dict = useDictStore();
  const loading = ref(false);
  const showModal = ref(false);
  const formValue = ref<State>(newState(null));
  const formRef = ref<any>({});
  const formBtnLoading = ref(false);
  const dialogWidth = computed(() => {
    return adaModalWidth(840);
  });

  // 提交表单
  function confirmForm(e) {
    e.preventDefault();
    formRef.value.validate((errors) => {
      if (!errors) {
        formBtnLoading.value = true;
        Edit(formValue.value)
          .then((_res) => {
            message.success('操作成功');
            closeForm();
            emit('reloadTable');
          })
          .finally(() => {
            formBtnLoading.value = false;
          });
      } else {
        message.error('请填写完整信息');
      }
    });
  }

  // 关闭表单
  function closeForm() {
    showModal.value = false;
    loading.value = false;
  }

  // 打开模态框
  function openModal(state: State) {
    showModal.value = true;

    // 新增
    if (!state || state.id < 1) {
      formValue.value = newState(state);

      return;
    }

    // 编辑
    loading.value = true;
    View({ id: state.id })
      .then((res) => {
        formValue.value = res;
      })
      .finally(() => {
        loading.value = false;
      });
  }

  defineExpose({
    openModal,
  });
</script>

<style lang="less"></style>